//
//  ECUIVC_TextViewController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/3/12.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUIVC_TextViewController.h"
#import <WebKit/WebKit.h>
#import "ECUCatalogModel.h"

@interface ECUIVC_TextViewController ()<WKUIDelegate,WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *wkweb;

@end

@implementation ECUIVC_TextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar lt_setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:1]];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];

}
- (void)addSubviews
{
    [super addSubviews];
    self.view.backgroundColor = [UIColor whiteColor];

    // 自适应屏幕宽度js
//    NSString *jScript =
//    @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
    NSString *jScript =
        @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (let img of imgs){img.style.maxWidth='100%';img.style.height='auto';}";
//    NSString *jScript =
//    @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i = 0; i < imgs.length ;i++){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
    
    WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    [wkUController addUserScript:wkUScript];
    WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
    wkWebConfig.userContentController = wkUController;
    self.wkweb = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavHeight) configuration:wkWebConfig];
    self.wkweb.navigationDelegate = self;
    [self.view addSubview:self.wkweb];
    
    [self.wkweb loadHTMLString:self.foldCellModel.data.content baseURL:nil];

}
- (void)addBarButtons
{
    [super addBarButtons];
    [self.navigationController.navigationBar lt_setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:1]];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    //发送成功,改变对应状态
    [[NSNotificationCenter defaultCenter]postNotificationName:kNotifactionTextLearned object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
